package com.dbs.exceptions;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * web层统一异常处理view版
 * 
 * @author dongbisheng
 * @date Apr 13, 2017 1:09:50 PM
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	public static final String DEFAULT_ERROR_VIEW = "error";

	/*@ExceptionHandler(value = Exception.class)
	public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.addObject("exception", e);
		mav.addObject("url", req.getRequestURL());
		mav.setViewName(DEFAULT_ERROR_VIEW);
		mav.addObject("test", "统一异常页面");
		return mav;
	}*/

	@ExceptionHandler(value = Exception.class)
	@ResponseBody // restful版多加这个
	public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		ErrorInfo<String> err = new ErrorInfo<String>();
		err.setData(String.class.toString());
		err.setCode(ErrorInfo.ERROR);
		err.setMessage(e.getMessage());
		err.setUrl(req.getRequestURI().toString());
		return err;
	}
}
